using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace FirebirdDAO
{
    ///<summary>
    /// DAO Class for the Country table.
    ///</summary>
    public class Country : Entity
    {
        private String _COUNTRY;
        private String _CURRENCY;

        ///Default constructor
        public Country()
        {

        }

        /// <summary>
        /// Constructor for joins. 
        /// Available CHILD tables to join with:
        ///    - Job ( Job_integ_11 )
        ///    - Customer ( Customer_integ_61 )
        /// </summary>
        public Country(bool Flat, params Entity[] entities)
            : this()
        {
            foreach (Entity entity in entities)
            {
                string[] entityParts = entity.GetType().ToString().Split('.');
                string entityName = entityParts[entityParts.Length - 1];
                PropertyInfo pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, new EntityCollection(entity), null);
                }
                entityName = entityName + "_";
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
            }
            SetFlat(Flat);
        }

        ///Constructor for Joins with flat hierarchy
        public Country(params Entity[] entities)
            : this(true, entities)
        {
        }

        ///<summary>
        /// Property for column Country.
        ///</summary>
        [IsNotNullable]
        [IsPK]
        [MaxLength(15)]
        public String Countryy
        {
            get
            {
                return _COUNTRY;
            }
            set
            {
                _COUNTRY = value;
            }
        }
        ///<summary>
        /// Property for column Currency.
        ///</summary>
        [IsNotNullable]
        [MaxLength(10)]
        public String Currency
        {
            get
            {
                return _CURRENCY;
            }
            set
            {
                _CURRENCY = value;
            }
        }

        #region Job
        private EntityCollection _Job;
        ///<summary>
        /// Related table Job as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(Job), "Job")]
        public EntityCollection Job
        {
            get
            {
                return _Job;
            }

            set
            {
                _Job = value;
            }
        }
        private Job _Job_;
        ///<summary>
        /// Related table Job as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public Job Job_
        {
            get
            {
                return _Job_;
            }

            set
            {
                _Job_ = value;
            }
        }
        #endregion

        #region Customer
        private EntityCollection _Customer;
        ///<summary>
        /// Related table Customer as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(CUSTOMER), "Customer")]
        public EntityCollection Customer
        {
            get
            {
                return _Customer;
            }

            set
            {
                _Customer = value;
            }
        }
        private CUSTOMER _Customer_;
        ///<summary>
        /// Related table Customer as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public CUSTOMER Customer_
        {
            get
            {
                return _Customer_;
            }

            set
            {
                _Customer_ = value;
            }
        }
        #endregion
        /// <summary>
        /// Gets join for current entity and table.
        /// </summary>
        /// <param name="tableName">Table to join with entity.</param>
        /// <returns>Join definition for entity and table.</returns>
        public override DBJoin GetJoin(string tableName)
        {
            if (string.Compare(tableName, "Job", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Job_country");
                right.Add(GetType().Name + ".Country");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            if (string.Compare(tableName, "Customer", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Country");
                right.Add(GetType().Name + ".Country");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            return null;
        }
    }
}


